﻿namespace DotRas
{
    using pptpclient.Properties;
    using System;

    public sealed class RasEntryNameValidator
    {
        private bool _allowExistingEntries;
        private bool _allowNonExistantPhoneBook;
        private string _entryName;
        private int _errorCode;
        private string _errorMessage;
        private bool _isValid;
        private string _phoneBookPath;

        public void Validate()
        {
            try
            {
                int errorCode = SafeNativeMethods.RasValidateEntryName(this.PhoneBookPath, this.EntryName);
                if (((errorCode == 0) || (this.AllowExistingEntries && (errorCode == 0xb7))) || (this.AllowNonExistantPhoneBook && (errorCode == 0x26d)))
                {
                    this._errorCode = 0;
                    this._errorMessage = null;
                }
                else
                {
                    this._errorCode = errorCode;
                    this._errorMessage = RasHelper.GetRasErrorString(errorCode);
                }
                this._isValid = this._errorCode == 0;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
        }

        public bool AllowExistingEntries
        {
            get
            {
                return this._allowExistingEntries;
            }
            set
            {
                this._allowExistingEntries = value;
            }
        }

        public bool AllowNonExistantPhoneBook
        {
            get
            {
                return this._allowNonExistantPhoneBook;
            }
            set
            {
                this._allowNonExistantPhoneBook = value;
            }
        }

        public string EntryName
        {
            get
            {
                return this._entryName;
            }
            set
            {
                this._entryName = value;
            }
        }

        public int ErrorCode
        {
            get
            {
                return this._errorCode;
            }
        }

        public string ErrorMessage
        {
            get
            {
                return this._errorMessage;
            }
            set
            {
                this._errorMessage = value;
            }
        }

        public bool IsValid
        {
            get
            {
                return this._isValid;
            }
            set
            {
                this._isValid = value;
            }
        }

        public string PhoneBookPath
        {
            get
            {
                return this._phoneBookPath;
            }
            set
            {
                this._phoneBookPath = value;
            }
        }
    }
}

